package com.yilijk.base.base;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.yilijk.base.R;
import com.yilijk.base.databinding.BaseDialogBase2Binding;


/**
 * @Description: 公用对话框
 * @Author: cenjunlei
 * @Date: 2021/7/23 17:32
 */
public class BaseDialog extends Dialog {
    public BaseDialog(@NonNull Context context) {
        super(context);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);

    }

    public static class Builder {
        private final Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private boolean showCloseButton;
        private View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private boolean mCancelable = true;
        private int messageGravity = -1;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public void setCanceledOnTouchOutside(boolean cancelable) {
            this.mCancelable = cancelable;
        }

        public void setShowCloseButton(boolean showCloseButton) {
            this.showCloseButton = showCloseButton;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setMessageGravity(int messageGravity) {
            this.messageGravity = messageGravity;
            return this;
        }

        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public BaseDialog create() {
            final BaseDialog dialog = new BaseDialog(context, R.style.Dialog);
            BaseDialogBase2Binding binding = BaseDialogBase2Binding.inflate(LayoutInflater.from(context));
            dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            dialog.setCanceledOnTouchOutside(mCancelable);
            if (negativeButtonText == null) {
                binding.tvNegative.setVisibility(View.GONE);
            }

            if (positiveButtonText == null) {
                binding.tvPositive.setVisibility(View.GONE);
            }

            if (negativeButtonText == null || positiveButtonText == null) {
                binding.viewLineV.setVisibility(View.GONE);
            }

            if (negativeButtonText == null && positiveButtonText == null) {
                binding.viewLineV.setVisibility(View.GONE);
                binding.viewLineH.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(title)) {
                binding.tvTitle.setText(title);
            } else {
                binding.tvTitle.setVisibility(View.GONE);
            }

            if (contentView != null) {
                binding.llContent.removeAllViews();
                binding.llContent.addView(contentView);
            } else {
                if (messageGravity > 0) {
                    binding.tvContent.setGravity(messageGravity);
                }
                binding.tvContent.setText(message);
            }

            binding.tvNegative.setText(negativeButtonText);
            binding.tvNegative.setOnClickListener(v -> {
                if (negativeButtonClickListener != null) {
                    negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            });
            binding.tvPositive.setText(positiveButtonText);
            binding.tvPositive.setOnClickListener(v -> {
                if (positiveButtonClickListener != null) {
                    positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                }
            });

            if (showCloseButton) {
                binding.ivClose.setVisibility(View.VISIBLE);
                binding.ivClose.setOnClickListener(v -> {
                    dialog.dismiss();
                });
            }
            return dialog;
        }
    }
}
